#include <stdio.h>
#include <string.h>

#include "AC33Mxxx_conf.h"
#include "driver_header.h"
#include "test_header.h"
#include "AC33Mxxx_it.h"

#include "console.h"
#include "slib.h"



int IO_OUT_TEST_H()
{

	CSP_GPIO_OutputHigh(GPIO_A,PIN_0);
	CSP_GPIO_OutputHigh(GPIO_B,PIN_0);
	CSP_GPIO_OutputHigh(GPIO_D,PIN_0);

	return 0 ;
}


int IO_OUT_TEST_L()
{

	CSP_GPIO_OutputLow(GPIO_A,PIN_0);
	CSP_GPIO_OutputLow(GPIO_B,PIN_0);
	CSP_GPIO_OutputLow(GPIO_D,PIN_0);

	return 0 ;
}


//#############################################################################
//#
//#
//#
//#############################################################################
void IO_Test(void)
{
	char 	ch;
	int		service_flag; 
	int		cmd_not_supported; 
	int		disp_menu = 1; 
	char 	ARIA_vihvil = 0;

	
	cputs(">>>>> IO Test <<<<<\r\n"); 


	//-----------------------------------------------------------------------
	// init variables
	//-----------------------------------------------------------------------
	service_flag = 1; 
	cmd_not_supported = 0; 
	InFlag = InCount = 0; 


	//########################################################
	// test body 
	//########################################################
	while (service_flag)
	{

		//-------------------------------------------------------------------
		// disp menu
		//-------------------------------------------------------------------
		if (disp_menu)
		{
			Disp_IO_Menu(); 
			disp_menu = 0; 
		}


		
		cputs("\r\nIO>"); 		


		//-------------------------------------------------------------------
		// clear command buffer
		//-------------------------------------------------------------------
		InData[0] = 0; 


		//-------------------------------------------------------------------
		// get command
		//-------------------------------------------------------------------
		for (;;)
		{
			ch = getstring(); 
			if (ch == ASCII_CARRIAGE_RETURN) break; 

			if(ARIA_vihvil)
			{
				if(CSP_GPIO_GET_PnIDR(GPIO_A) & 0x01)
					CSP_GPIO_OutputHigh(GPIO_D,PIN_5);
				else
					CSP_GPIO_OutputLow(GPIO_D,PIN_5);

				if(CSP_GPIO_GET_PnIDR(GPIO_B) & 0x01)
					CSP_GPIO_OutputHigh(GPIO_D,PIN_6);
				else
					CSP_GPIO_OutputLow(GPIO_D,PIN_6);

				if(CSP_GPIO_GET_PnIDR(GPIO_D) & 0x01)
					CSP_GPIO_OutputHigh(GPIO_D,PIN_7);
				else
					CSP_GPIO_OutputLow(GPIO_D,PIN_7);
			}
		
		}

		cputs("\r\n"); 


		//------------------------------------------------------------------
		//  collection of services 
		//------------------------------------------------------------------
		if (InFlag)
		{
			if (!strncmp(InData, "exit", 4))
			{
				service_flag = 0; 
			}
			else if (!strncmp(InData, "input", 5))
			{
				cmd_not_supported = IO_INPUT_TEST_set(); 
				ARIA_vihvil = 0x77;
			}
			else if (!strncmp(InData, "outh", 4))
			{
				cmd_not_supported =IO_OUT_TEST_H(); 
				ARIA_vihvil = 0;
			}
			else if (!strncmp(InData, "outl", 4))
			{
				cmd_not_supported = IO_OUT_TEST_L(); 
				ARIA_vihvil = 0;
			}
			else if (!strncmp(InData, "out", 3))
			{
				cmd_not_supported = IO_OUT_TEST_set(); 
				ARIA_vihvil = 0;
			}
			else if (!strncmp(InData, "h", 1))
			{
				disp_menu = 1; 
				ARIA_vihvil = 0;
			}
			else 
			{
				cmd_not_supported = 1; 
			}


			if (cmd_not_supported)
			{
				cmd_not_supported = 0; 
				cputs("This command is not supported...\r\n"); 
			}

		}

		
		InFlag = InCount = 0; 
	}

	cputs("IO Test Exit...\r\n\r\n"); 
	InFlag = InCount = 0; 
}



/**
*****************************************************************************************************
* @ Name : Disp_GPIO_Menu
*
*
*
*****************************************************************************************************
*/
int  Disp_IO_Menu (void)
{

	cprintf("	   \r\n");
	cprintf("==[IO]===========================\r\n"); 
	cprintf("command: init, test\r\n");
	cprintf(" input , out < outh , outl > \r\n");
	cprintf("[h] for displaying menu again\r\n");
	cprintf("===================================\r\n\r\n"); 

	return 0;
}


int IO_OUT_TEST_set()
{
	//-------------------------------------------------------------------------
	//
	//-------------------------------------------------------------------------
	CSP_PCU_ConfigureFunction(PCU_A, PIN_0, PA0_MUX_PA0); 
	CSP_PCU_Set_Direction_Type(PCU_A, PIN_0, PnCR_OUTPUT_PUSH_PULL);
	CSP_PCU_ConfigurePullup(PCU_A, PIN_0, PnPCR_PULLUP_DISABLE); 

	CSP_PCU_ConfigureFunction(PCU_B, PIN_0, PB0_MUX_PB0); 
	CSP_PCU_Set_Direction_Type(PCU_B, PIN_0, PnCR_OUTPUT_PUSH_PULL);
	CSP_PCU_ConfigurePullup(PCU_B, PIN_0, PnPCR_PULLUP_DISABLE); 

	CSP_PCU_ConfigureFunction(PCU_D, PIN_0, PD0_MUX_PD0); 
	CSP_PCU_Set_Direction_Type(PCU_D, PIN_0, PnCR_OUTPUT_PUSH_PULL);
	CSP_PCU_ConfigurePullup(PCU_D, PIN_0, PnPCR_PULLUP_DISABLE); 


	return 0;
}

int IO_INPUT_TEST_set()
{

	CSP_PCU_ConfigureFunction(PCU_A, PIN_0, PA0_MUX_PA0); 
	CSP_PCU_ConfigureFunction(PCU_B, PIN_0, PB0_MUX_PB0); 
	CSP_PCU_ConfigureFunction(PCU_D, PIN_0, PD0_MUX_PD0); 
	CSP_PCU_ConfigureFunction(PCU_D, PIN_5, PC0_MUX_PC0); 
	CSP_PCU_ConfigureFunction(PCU_D, PIN_6, PC0_MUX_PC0); 
	CSP_PCU_ConfigureFunction(PCU_D, PIN_7, PC0_MUX_PC0); 


	CSP_PCU_Set_Direction_Type(PCU_A, PIN_0, PnCR_INPUT_LOGIC);
	CSP_PCU_Set_Direction_Type(PCU_B, PIN_0, PnCR_INPUT_LOGIC);
	CSP_PCU_Set_Direction_Type(PCU_D, PIN_0, PnCR_INPUT_LOGIC);
	CSP_PCU_Set_Direction_Type(PCU_D, PIN_5, PnCR_OUTPUT_PUSH_PULL);
	CSP_PCU_Set_Direction_Type(PCU_D, PIN_6, PnCR_OUTPUT_PUSH_PULL);
	CSP_PCU_Set_Direction_Type(PCU_D, PIN_7, PnCR_OUTPUT_PUSH_PULL);


	CSP_PCU_ConfigurePullup(PCU_A, PIN_0, PnPCR_PULLUP_DISABLE); 
	CSP_PCU_ConfigurePullup(PCU_B, PIN_0, PnPCR_PULLUP_DISABLE); 
	CSP_PCU_ConfigurePullup(PCU_D, PIN_0, PnPCR_PULLUP_DISABLE); 
	CSP_PCU_ConfigurePullup(PCU_D, PIN_5, PnPCR_PULLUP_DISABLE); 
	CSP_PCU_ConfigurePullup(PCU_D, PIN_6, PnPCR_PULLUP_DISABLE); 
	CSP_PCU_ConfigurePullup(PCU_D, PIN_7, PnPCR_PULLUP_DISABLE); 

	return 0;
}

